
class Error:
  def __init__(self,sample,path,message):
    self.sample = sample
    self.path = path
    self.message = str(message).rstrip()



class ErrorCollator:

  def __init__(self):
    self.errors = []

  def add(self,error):
    self.errors.append(error)

  def hasErrors(self):
    if len(self.errors)<=0:
      return False
    return True

  def dump(self):
    for error in self.errors:
       print error.sample,error.path,error.message

  def emailAlert(self, source, notifyList, smtp_server="localhost",
         subject = "ICAT metadata scraping errors"):
    if len(self.errors)<=0:
      return
    import smtplib
    
    SERVER = smtp_server 
    FROM = source
    TO = notifyList
    SUBJECT = subject
    TEXT = "This message was sent with Python's smtplib."
    message = """\
    From: %s
    To: %s
    Subject: %s
    
    %s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    elist =[message]
    for error in self.errors:
       elist.append( " ".join([error.sample, error.path, error.message, "\n"]))
    error_s = "".join(elist)
    # Send the mail
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, error_s)
    server.quit()
